﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.RM.Bll;
using CIT.WMS.Product.Bll;

namespace CIT.WMS.RM
{
    public partial class FrmShouhuo_1 : Form
    {
        public FrmShouhuo_1()
        {
            InitializeComponent();
        }

        string _BillNo = "";
        string _RowNo = "";
        string _PartNumber = "";
        decimal _InQty = 0;
        string _DateCode = "";
        string _Xuehao = "";
        string _Updator = "";
        string _BarCode = "";

        private void FrmRMOut_Load(object sender, EventArgs e)
        {
            InitFrm();
            txtBillNo.Focus();
        }

        /// <summary>
        /// 单号输入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtBillNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                InitFrm();
                BillNoChange();
            }
        }

        /// <summary>
        /// 输入单号
        /// </summary>
        private void BillNoChange()
        {
            try
            {
                InitFrm();

                _BarCode = txtBillNo.Text;
                var code = txtBillNo.Text;
                var codeContent = code.Split(',');
                txtBillNo.Text = "";
                DataTable dtBills = new DataTable();

                //***条码验证START...
                if ((codeContent.Length == 8 || codeContent.Length == 1) == false)
                {
                    areaMsg.Text = "条码分割后长度错误，不满足8位";
                    return;
                }

                string sqlRow = "";
                string po, strQty, seq, ver;

                if (codeContent.Length == 8)
                {
                    //...1：序号
                    seq = codeContent[0];

                    //...2：采购单check
                    po = codeContent[1];
                    if (po.Length == 9 || po.Length == 6)
                    {
                    }
                    else
                    {
                        areaMsg.Text = "条码格式异常:采购订单长度错误";
                        return;
                    }
                    //...3：物料
                    _PartNumber = codeContent[2].Trim();
                    if (_PartNumber.Length == 0)
                    {
                        areaMsg.Text = "条码格式异常:物料不能为空";
                        return;
                    }

                    //...4：版本
                    ver = codeContent[3].Trim();
                    if (ver.Length == 0)
                    {
                        areaMsg.Text = "条码格式异常:版本不能为空";
                        return;
                    }

                    //...5：数量
                    strQty = codeContent[4];
                    try
                    {
                        _InQty = Convert.ToDecimal(strQty);
                    }
                    catch
                    {
                        areaMsg.Text = "条码格式异常:数量异常";
                        return;
                    }

                    //...6：DateCodeCheck
                    _DateCode = codeContent[5];
                    if (IsDateTime(_DateCode) == false)
                    {
                        areaMsg.Text = "条码格式异常:DataCode格式错误";
                        return;
                    }

                    //...7：穴号Check
                    _Xuehao = codeContent[6];

                    sqlRow = " and r.RelatedBill='{0}' and r.PartNumber='         {1}' ";
                    sqlRow = string.Format(sqlRow, po, _PartNumber);
                    dtBills = Bll_Shouhuo.GetRowInfo(sqlRow);
                }

                if (codeContent.Length == 1)
                {
                    var cb = codeContent[0].Split('-');
                    if (cb.Length > 1)
                    {
                        sqlRow = " and r.BillNo='{0}' and r.RowNo={1}";
                        sqlRow = string.Format(sqlRow, cb[0], cb[1]);
                    }
                    else
                    {

                        sqlRow = " and r.BillNo='{0}' ";
                        sqlRow = string.Format(sqlRow, cb[0]);
                    }
                    dtBills = Bll_Shouhuo.GetRowInfo(sqlRow);
                }
                //***条码验证END


                if (dtBills.Rows.Count == 0)
                {
                    areaMsg.Text = "查无此单号，请确认单号是否正确";
                    return;
                }

                if (dtBills.Rows[0]["Flg"].ToString() != "0")
                {
                    areaMsg.Text = "此订单行已收货";
                    return;
                }

                listBills.DisplayMember = "CbNo";
                listBills.ValueMember = "CbNo";
                listBills.DataSource = dtBills;
                listBills.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 初始化画面
        /// </summary>
        private void InitFrm()
        {
            _BillNo = "";
            areaInfo.Text = "";
            areaMsg.Text = "";
            _PartNumber = "";
            _InQty = 0;
            _DateCode = "";
            _Xuehao = "";
            _Updator = "";
            _BarCode = "";

            listBills.DataSource = null;
            txtBillNo.Focus();
        }

        /// <summary>
        /// 判断时间格式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private bool IsDateTime(string str)
        {
            System.IFormatProvider format = new System.Globalization.CultureInfo("zh-CN", true);
            try
            {
                DateTime.ParseExact(str, "yyyyMMdd", format);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void btnJump_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_BillNo))
            {
                areaMsg.Text = "请先选择单号";
                return;
            }

            if (string.IsNullOrEmpty(_Updator) == false)
            {
                if (_Updator.Equals(Utils.uContext.UserNamek__BackingField) == false)
                {
                    areaMsg.Text = "此订单行已被" + _Updator + "锁定";
                    return;
                }
            }

            if (Bll_Shouhuo.Bills.Contains(_BillNo))
            {
                var ret = MessageBox.Show("此订单本次操作中已有收货记录，继续录入选择取消？", "消息提示",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                if (ret == DialogResult.OK)
                {
                    return;
                }
            }

            //var ret = MessageBox.Show("是否进行收货？", "消息提示",
            //    MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            //if (ret == DialogResult.OK)
            {
                //锁定订单
                Bll_Shouhuo.LockRow(_BillNo, _PartNumber, Utils.uContext.UserNamek__BackingField);

                FrmShouhuo_2 frm = new FrmShouhuo_2(_BillNo, _RowNo, _InQty, _DateCode, _Xuehao, _BarCode);
                var ret = frm.ShowDialog();
                if (ret == DialogResult.OK)
                {
                    areaMsg.Text = _BillNo + "," + _RowNo + "   收货成功";
                }
                //InitFrm();
            }
        }

        private void listBills_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBills.SelectedIndex > -1)
            {
                var val = listBills.SelectedValue.ToString();
                _BillNo = val.Split(',')[0];
                _RowNo = val.Split(',')[1];
                var sqlRow = " and r.BillNo='{0}' and r.RowNo={1}";
                sqlRow = string.Format(sqlRow, _BillNo, _RowNo);
                var dtBills = Bll_Shouhuo.GetRowInfo(sqlRow);
                if (dtBills.Rows.Count == 0)
                {
                    areaMsg.Text = "查无此单号，请确认单号是否正确";
                    return;
                }

                _Updator = dtBills.Rows[0]["Updator"].ToString();

                var strInfo = "订单详情:\r\n";
                strInfo += "采购订单>" + dtBills.Rows[0]["RelatedBill"].ToString() + "," + _RowNo + "行\r\n";
                strInfo += "物料编号>" + dtBills.Rows[0]["PartNumber"].ToString().Trim() + " " + dtBills.Rows[0]["Version"].ToString() + "\r\n";
                strInfo += "订单数量>" + dtBills.Rows[0]["PlanQty"].ToString() + " " + dtBills.Rows[0]["Unit"].ToString() + "\r\n";
                areaInfo.Text = strInfo;
                areaMsg.Text = "";
            }
        }
        //END
    }
}